package com.carkeeper.mongo.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.mongo.pojo.Appraisal;
import com.carkeeper.mongo.service.MongoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Api(tags = "MongoController",description = "服务评价管理")
@RestController
@RequestMapping("/mongo")
public class MongoController {

    @Autowired
    private MongoService mongoService;

    /**
     * mongo库中
     */
    @ApiOperation("存储所有未被删除的评价" )
    @PostMapping("/saveAll")
    public void saveAll(){
        mongoService.saveAll();
    }

    @ApiOperation("分页查询评价" )
    @GetMapping("/find")
    public PageResult<Appraisal> find(@RequestParam(value = "currPage",defaultValue = "1",required = false) Integer currPage, @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize){
        try {
            PageResult<Appraisal> page = mongoService.find(currPage,pageSize);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }

    /**
     * 根据订单id查询
     * @param orderId
     * @return
     */
    @ApiOperation("根据orderId查询评价" )
    @GetMapping("/findOne/{orderId}")
    public Result<Appraisal> findOne(@PathVariable("orderId") String orderId){
        try {
            Appraisal appraisal =  mongoService.findOne(orderId);
            return new Result<Appraisal>(true, StatusCode.OK,"查询成功",appraisal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<Appraisal>(false, StatusCode.ERROR,"查询失败");

        }
    }

    /**
     * 根据订单id删除
     * @param orderId
     * @return
     */
    @ApiOperation("根据orderId删除评价" )
    @DeleteMapping("/delete/{orderId}")
    public Result delete(@PathVariable("orderId") String orderId){
        try {
            mongoService.delete(orderId);
            return new Result<Appraisal>(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<Appraisal>(false, StatusCode.ERROR,"删除失败");

        }
    }

    /**
     * 模糊查询加分页
     * @param searchMap
     * @param currPage
     * @param pageSize
     * @return
     */
    @ApiOperation("根据条件分页查询评价" )
    @GetMapping("/search/{currPage}/{pageSize}")
    public PageResult<Appraisal>  search(@RequestParam Map<String,Object> searchMap,@PathVariable("currPage") Integer currPage,@PathVariable("pageSize") Integer pageSize){
        try {

            PageResult<Appraisal> page = mongoService.search(searchMap, currPage, pageSize);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
}
